import Vue from 'vue'
import axios from 'axios'
import baseUrl from '../config/index.js'
import { Message } from 'element-ui';
Vue.prototype.$axios = axios
axios.defaults.timeout = 10000
axios.defaults.baseURL = baseUrl
axios.interceptors.request.use(
  config => {
    // 配置token
    let token = localStorage.getItem('token');
    if (token) {
      config.headers['token'] = token
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  })

axios.interceptors.response.use(   
  response => {  //成功请求到数据
    // //这里根据后端提供的数据进行对应的处理
    if (response.data.code == 200) {
      return response.data
    } else if (response.data.code == -300) {
      localStorage.clear()
      Message.error('请重新登录')
      location.replace('/admin/#/'); // 跳转登录页
      return Promise.reject(response);
    } else if (response.data.code == -400) {
      Message.error('请重新登录')
      localStorage.clear()
      location.replace('/admin/#/'); // 跳转登录页
      return Promise.reject(response);
    } else if(response.data.code == -100){
      Message.error(response.data.message)
      return Promise.reject(response);
    } else {
      return Promise.reject(response);
    }
  },
  error => {  //响应错误处理
    Message.error(error.message)
    return Promise.reject(error)
  }
)




